package com.longcheer.weibo.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.util.Log;

public class ImageUtils {
    
    private static String TAG = "PW_ImageUtils";
    
    private static BasicHttpParams params;
    
    static {
        params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 30*1000);
        HttpConnectionParams.setSoTimeout(params, 30*1000);
        HttpConnectionParams.setSocketBufferSize(params, 8096);
    }
    
    public static InputStream getImageStreamFromHTTP(String url) {
        HttpClient client = getHttpClient();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse resp = client.execute(get);
            if (resp.getStatusLine().getStatusCode() == 200) {
                return resp.getEntity().getContent();
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, "download image error.", e);
        } catch (IOException e) {
            Log.e(TAG, "download image error.", e);
        } 
        
        return null;
    }
    
    public static byte[] getImageFromHTTP(String url) {
        HttpClient client = getHttpClient();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse resp = client.execute(get);
            if (resp.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toByteArray(resp.getEntity()); 
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, "download image error.", e);
        } catch (IOException e) {
            Log.e(TAG, "download image error.", e);
        } 
        
        return null;
    }
    
    public static boolean storeImageFromHTTP(Context context, String storeName, String imageUrl) {
        boolean stored = false;
        
        InputStream in = null;
        OutputStream out = null;
        
        in = getImageStreamFromHTTP(imageUrl);
        try {
            if (in != null) {
                out = context.openFileOutput(storeName, Context.MODE_PRIVATE);
                IOUtils.copy(in, out);
            }
            stored = true;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "store Image File error", e);
        } catch (IOException e) {
            Log.e(TAG, "store Image File error", e);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        
        return stored;
    }
    
    private static HttpClient getHttpClient() {
        return new DefaultHttpClient(params);
    } 
}
